/**
 * a script of a dialog box used before deleting a student
 */

$(document).ready(function(){
		$("#removecourse").button();
		$("#registertocourse").button();
		$("#removecourse").click(function(){
		  var name = $(this).attr('name');
		  $("#dialog").dialog({ buttons: [
		                                   {
		                                       text: "Ok",
		                                       click: function() { 
		                                    	   $.ajax("/salonmorandsmigi/CourseServlet?action=delete&name="+name,{
		                                    		   type: "GET",
		                                    		   success: function(){
		                                    		     alert("succeeded deleting");
		                                    		     window.location.replace("/salonmorandsmigi/courses.jsp");
		                                    		   },
		                                    		   error: function(){
		                                    			 alert("error deleting");  
		                                    		   }
		                                    		 });
		                                    	   $(this).dialog("close");
		                                       }
		                                   },
		                                   {
		                                	   text: "Cancel",
		                                	   click: function() { $(this).dialog("close");}
		                                   }
		                                  ], 
		  						show: "fold",
		  						hide: "explode"
		  					 });
		});
		$("#registertocourse").click(function(){
			  var username = $(".username").attr('id');
			  var coursename = $(".coursename").attr('id');
			  $("#dialog2").dialog({ buttons: [
			                                   {
			                                       text: "Ok",
			                                       click: function() { 
			                                    	   $.ajax("/salonmorandsmigi/UserServlet?action=registerusercourse&course="+coursename+"&username="+username,{
			                                    		   type: "GET",
			                                    		   success: function(){
			                                    		     alert("Registration succeeded!");
			                                    		     window.location.replace("/salonmorandsmigi/student/mycourses.jsp");
			                                    		   },
			                                    		   error: function(){
			                                    			 alert("error Registering, try again later!");  
			                                    		   }
			                                    		 });
			                                    	   $(this).dialog("close");
			                                       }
			                                   },
			                                   {
			                                	   text: "Cancel",
			                                	   click: function() { $(this).dialog("close");}
			                                   }
			                                  ], 
			  						show: "fold",
			  						hide: "explode"
			  					 });
			});
});